﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class EquipForm : Form
    {
        MDIParent mdipar = new MDIParent();
        Equipment eqp = new Equipment();
        PMSContext dbcontext = new PMSContext();
        FormHelpers help = new FormHelpers();
        private string Datamanip;
        private int rowselect;

        public EquipForm()
        {
            InitializeComponent();
        }

        private void EquipForm_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();
        }
   
        #region Functions
        private void RefreshDB()
        {
            var eq = (from e in dbcontext.Equipments
                       select e).ToList();

            LVEquipInfo.Items.Clear();
            if (eq != null)
            {

                foreach (var sqlEq in eq)
                {
                    ListViewItem lvitems = LVEquipInfo.Items.Add(sqlEq.Equipmentid_PK.ToString());
                    lvitems.SubItems.Add(sqlEq.Equip_Name.ToString());
                    lvitems.SubItems.Add(sqlEq.Brand.ToString());
                    lvitems.SubItems.Add(sqlEq.Quantity.ToString());
                    lvitems.SubItems.Add(sqlEq.Description.ToString());

                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtEName.Clear();
            txtBrand.Clear();
            txtQty.Clear();
            txtDesc.Clear();
            txtSearch.Clear();

        }

        private void DisableFields()
        {
            txtEName.Enabled = false;
            txtBrand.Enabled = false;
            txtQty.Enabled = false;
            txtDesc.Enabled = false;
        }

        private void EnableFields()
        {
            txtEName.Enabled = true;
            txtBrand.Enabled = true;
            txtQty.Enabled = true;
            txtDesc.Enabled = true;
        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnNew.Enabled = true;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
            btnClear.Enabled = true;
            btnSave.Enabled = false;
        }
        #endregion

        private void btnNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";

            EnableFields();
            ClearFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            LVEquipInfo.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if ((txtEName.Text == "") || (txtQty.Text == ""))
            {
                MessageBox.Show("Please fill up the empty fields", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


                //validation for special character inputs
            else if (help.SpecChar(txtEName.Text, txtEName) == false || help.SpecChar(txtBrand.Text, txtBrand) == false || help.SpecChar(txtQty.Text, txtQty) == false)
            {

                MessageBox.Show("Special Characters are not allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (Datamanip == "Add")
                {

                    var eq = (from eqmt in dbcontext.Equipments
                              where eqmt.Equip_Name == txtEName.Text && eqmt.Brand == txtBrand.Text
                              select eqmt).SingleOrDefault();

                    if (eq == null)
                    {
                        eqp.Equip_Name = txtEName.Text;
                        eqp.Description = txtDesc.Text;
                        eqp.Brand = txtBrand.Text;
                        eqp.Quantity = Convert.ToInt32(txtQty.Text);
                        eqp.QtyUsed = 0;
                        dbcontext.Entry(eqp).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                    }
                    else
                    {
                        if (eq.Equip_Name == txtEName.Text)
                        {
                            DialogResult YN = MessageBox.Show("You've entered an existing equipment, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                            if (YN == DialogResult.Yes)
                            {
                                var replace = dbcontext.Equipments.Find(eq.Equipmentid_PK);
                                replace.Equip_Name = txtEName.Text;
                                replace.Description = txtDesc.Text;
                                replace.Brand = txtBrand.Text;
                                replace.Quantity = Convert.ToInt32(txtQty.Text);

                                dbcontext.Entry(replace).State = EntityState.Modified;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();
                            }
                        }
                        else
                        {
                            eqp.Equip_Name = txtEName.Text;
                            eqp.Description = txtDesc.Text;
                            eqp.Brand = txtBrand.Text;
                            eqp.Quantity = Convert.ToInt32(txtQty.Text);

                            dbcontext.Entry(eqp).State = EntityState.Added;
                            dbcontext.SaveChanges();

                            MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            RefreshFields();
                        }
                    }
                    LVEquipInfo.Enabled = true;
                }



                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var replace = dbcontext.Equipments.Find(Convert.ToInt32(txtID.Text));
                        replace.Equip_Name = txtEName.Text;
                        replace.Brand = txtBrand.Text;
                        replace.Quantity = Convert.ToInt32(txtQty.Text);
                        replace.Description = txtDesc.Text;

                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }



            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LVEquipInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVEquipInfo.FocusedItem.Index;


            txtID.Text = LVEquipInfo.Items[rowselect].SubItems[0].Text;
            txtEName.Text = LVEquipInfo.Items[rowselect].SubItems[1].Text;
            txtBrand.Text = LVEquipInfo.Items[rowselect].SubItems[2].Text;
            txtQty.Text = LVEquipInfo.Items[rowselect].SubItems[3].Text;
            txtDesc.Text = LVEquipInfo.Items[rowselect].SubItems[4].Text;
           

            btnDel.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
            btnNew.Enabled = true;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Another record will be affected, Do you still want to Delete it?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Equipments.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {
                    var pjeq = (from p in dbcontext.PrjEqs
                                where p.Equipid_FK == del.Equipmentid_PK
                                select p).SingleOrDefault();
                    if (pjeq != null)
                    {
                        dbcontext.Entry(pjeq).State = EntityState.Deleted;
                        dbcontext.SaveChanges();
                    }
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();

                    MessageBox.Show("Successfully deleted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            RefreshFields();
            RefreshDB();

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                RefreshDB();
            }
            else
            {
                //retrieve the same item name search by user
                var eq = (from p in dbcontext.Equipments
                             where p.Equip_Name.Contains(txtSearch.Text)
                             select p).ToList();

                LVEquipInfo.Items.Clear();
                if (eq.Count != 0)
                {

                    {
                        foreach (var sqlCus in eq)
                        {
                            ListViewItem lvitems = LVEquipInfo.Items.Add(sqlCus.Equipmentid_PK.ToString());
                            lvitems.SubItems.Add(sqlCus.Equip_Name.ToString());
                            lvitems.SubItems.Add(sqlCus.Brand.ToString());
                            lvitems.SubItems.Add(sqlCus.Quantity.ToString());
                            lvitems.SubItems.Add(sqlCus.Description.ToString());
                        }
                    }
                }

            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVEquipInfo.Enabled = true;
        }

        private void LLAssgn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AssignEquipForm AForm = new AssignEquipForm();
            AForm.Show();
        }

        private void LLTrans_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            TransferEquipForm TForm = new TransferEquipForm();
            TForm.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            EquipRptForm eqrpt = new EquipRptForm();

            eqrpt.Show();
        }

    }
}
